/**
 * Copyright (C) 2018-2021
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.modules.distributor.rest;

import co.yixiang.dozer.service.IGenerator;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.aop.ForbidSubmit;
import co.yixiang.modules.distributor.domain.DistributorPrice;
import co.yixiang.modules.distributor.domain.YxDistributor;
import co.yixiang.modules.distributor.service.DistributorPriceService;
import co.yixiang.modules.distributor.service.YxDistributorService;
import co.yixiang.modules.distributor.service.dto.DistributorPriceQueryCriteria;
import co.yixiang.modules.product.domain.YxStoreProductAttrValue;
import co.yixiang.modules.product.service.YxStoreProductAttrValueService;
import co.yixiang.modules.product.service.dto.YxStoreProductAttrValueQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ysl
 * @date 2021-11-21
 */
@Api(tags = "商城:经销商管理")
@RestController
@RequestMapping("api")
public class DistributorPriceController {

    private final YxDistributorService yxDistributorService;
    private final YxStoreProductAttrValueService yxStoreProductAttrValueService;
    private final DistributorPriceService distributorPriceService;


    private final IGenerator generator;
    public DistributorPriceController(YxDistributorService yxDistributorService, YxStoreProductAttrValueService yxStoreProductAttrValueService,
                                      DistributorPriceService distributorPriceService, IGenerator generator) {
        this.yxDistributorService = yxDistributorService;
        this.yxStoreProductAttrValueService = yxStoreProductAttrValueService;
        this.distributorPriceService = distributorPriceService;
        this.generator = generator;
    }


    @Log("查询经销商价格")
    @ApiOperation(value = "查询经销商价格")
    @GetMapping(value = "/getDistributorPricesByDisId/{id}")
    public ResponseEntity getDistributorPricesByDisId(@PathVariable String id,Pageable pageable) {
        Map<String, Object> distributorPrices = distributorPriceService.queryDistributorPricesByDisId(id, pageable);

        return new ResponseEntity<>(distributorPrices,HttpStatus.OK);
    }

    @ApiOperation(value = "修改")
    @PutMapping(value = "/editDistributorPrice")
    public ResponseEntity updateDistributor(@Validated @RequestBody DistributorPrice resources){
        boolean result = distributorPriceService.addOrUpdate(resources);
        if (result) {
            return new ResponseEntity(HttpStatus.NO_CONTENT);
        } else {
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }


    @ForbidSubmit
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/delDistributorPrice/{id}")
    //@PreAuthorize("hasAnyRole('admin','YXARTICLE_ALL','YXARTICLE_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id) {
        boolean result = distributorPriceService.delOrDoNothing(id);
        if (result) {
            return new ResponseEntity(HttpStatus.NO_CONTENT);
        } else {
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
